//
//  WJFMDBTool.m
//  JerryIos
//
//  Created by 王军 on 2018/2/15.
//  Copyright © 2018年 王军. All rights reserved.
//

#import "WJFmdbUtils.h"
#import "FMDB.h"

@implementation WJFmdbUtils

//static作用：保证queue这个变量，只能被WJFmdbUtils.m直接访问
static FMDatabaseQueue *queue;

+(void)initialize {
    //获得沙盒中数据库文件名
    NSString *fileName = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"fmdb.sqlite"];
    //创建数据库队列
    queue = [FMDatabaseQueue databaseQueueWithPath:fileName];
    //创建表
    [queue inDatabase:^(FMDatabase *db) {
        NSString *tableSql = @"create table if not exists t_data (id integer primary key autoincrement, name text, age integer);";
        BOOL result = [db executeUpdate:tableSql];
        if (result) {
            NSLog(@"create table success");
        } else {
            NSLog(@"create table failed");
        }
    }];
}

+(void) addData:(WJFmdbData *)data {
    [queue inDatabase:^(FMDatabase *db) {
        NSString *insertSql = @"insert into t_data (name, age) values (?, ?);";
        BOOL result = [db executeUpdate:insertSql, data.name, @(data.age)];
        if (result) {
            NSLog(@"insert success");
        } else {
            NSLog(@"insert failed");
        }
    }];
}

+(void) updateData:(WJFmdbData *)data {
    [queue inDatabase:^(FMDatabase *db) {
        NSString *updateSql = @"update t_data set name = ?, age = ? where id = ?;";
        BOOL result = [db executeUpdate:updateSql, data.name, @(data.age), @(data.id)];
        if (result) {
            NSLog(@"update success");
        } else {
            NSLog(@"update failed");
        }
    }];
}

+(void) deleteData:(int) id {
    [queue inDatabase:^(FMDatabase *db) {
        NSString *deleteSql = @"delete from t_data where id = ?;";
        BOOL result = [db executeUpdate:deleteSql, @(id)];
        if (result) {
            NSLog(@"delete success");
        } else {
            NSLog(@"delete failed");
        }
    }];
}




+(void) addDataArray:(NSArray *)dataArray {
    [queue inTransaction:^(FMDatabase * _Nonnull db, BOOL * _Nonnull rollback) {
        for (WJFmdbData *data in dataArray) {
            NSString *insertSql = @"insert into t_data (name, age) values (?, ?);";
            BOOL result = [db executeUpdate:insertSql, data.name, @(data.age)];
            if (!result) {
                *rollback = YES;
                return;
            }
        }
    }];
}

+(void) updateDataArray:(NSArray *)dataArray {
    [queue inTransaction:^(FMDatabase * _Nonnull db, BOOL * _Nonnull rollback) {
        for (WJFmdbData *data in dataArray) {
            NSString *updateSql = @"update t_data set name = ?, age = ? where id = ?;";
            BOOL result = [db executeUpdate:updateSql, data.name, @(data.age), @(data.id)];
            if (!result) {
                *rollback = YES;
                return;
            }
        }
    }];
}

+(void) deleteDataArray:(NSArray *) idArray {
    [queue inTransaction:^(FMDatabase * _Nonnull db, BOOL * _Nonnull rollback) {
        for (int i=0; i<idArray.count; i++) {
            NSString *deleteSql = @"delete from t_data where id = ?;";
            BOOL result = [db executeUpdate:deleteSql, idArray[i]];
            if (!result) {
                *rollback = YES;
                return;
            }
        }
    }];
}




+(NSArray *) data {
    NSMutableArray *dataArray = [NSMutableArray array];
    [queue inDatabase:^(FMDatabase *db) {
        NSString *selectSql = @"select * from t_data;";
        FMResultSet *rs = [db executeQuery:selectSql];
        while (rs.next) {
            WJFmdbData *data = [[WJFmdbData alloc] init];
            data.id = [rs intForColumn:@"id"];
            data.name = [rs stringForColumn:@"name"];
            data.age = [rs intForColumn:@"age"];
            [dataArray addObject:data];
        }
    }];
    return dataArray;
}

@end
